package com.example.front.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.bumptech.glide.Glide;
import com.example.front.R;
import com.example.front.main.user.UserInformationActivity;
import com.example.front.sign.LoginActivity;
import com.google.android.material.navigation.NavigationView;

/**
 * 主界面活动类
 */
public class MainActivity extends AppCompatActivity {
    private NavHostFragment navHostFragment;
    private NavController navController;
    private AppBarConfiguration appBarConfiguration;
    private NavigationView navView;
    private ImageView nav_img;
    View headview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navHostFragment = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        navController = navHostFragment.getNavController();
        DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
        appBarConfiguration = new AppBarConfiguration.Builder(R.id.fragment1,R.id.fragment2,R.id.fragment3,R.id.fragment4,R.id.fragment5)
                .setDrawerLayout(drawerLayout)
                .build();
        NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);

        navView = findViewById(R.id.nav_view);
        NavigationUI.setupWithNavController(navView,navController);

        headview=navView.inflateHeaderView(R.layout.layout_nav_header);
        nav_img= (ImageView) headview.findViewById(R.id.iv_nav_img);

        SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        String userName = sharedPreferences.getString("username", "");
        String userHead = sharedPreferences.getString("photo", "");
        if (userHead.equals("null"))
            userHead = "";
        Glide.with(MainActivity.this)
                .load(userHead)
                .crossFade()
                .error(R.drawable.head)
                .into(nav_img);

        TextView userNameText = (TextView) headview.findViewById(R.id.tv_nav_txt);
        userNameText.setText(userName);
        nav_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, UserInformationActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        String userName = sharedPreferences.getString("username", "");
        TextView userNameText = (TextView) headview.findViewById(R.id.tv_nav_txt);
        userNameText.setText(userName);
        String userHead = sharedPreferences.getString("photo", "https://cloud.662359.cn/view.php/77851657c364e7e176fdd7816e311fa9.png");
        if (userHead.equals("null"))
            userHead = "https://cloud.662359.cn/view.php/77851657c364e7e176fdd7816e311fa9.png";
        Glide.with(MainActivity.this)
                .load(userHead)
                .crossFade()
                .error(R.drawable.head)
                .into(nav_img);
    }

    @Override
    public boolean onSupportNavigateUp(){
        return NavigationUI.navigateUp(navController,appBarConfiguration)||super.onSupportNavigateUp();
    }

    public void exit_textView(View view){
        SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private void call() {
        String phoneNumber = "18744891903";
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phoneNumber));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}